package vitaliy.bondar.diamondcave;

import java.io.IOException;
import java.util.Locale;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.KeyEvent;

public class GameActivity extends BaseGameActivity {
    
    private BoundCamera camera;
    
    public EngineOptions onCreateEngineOptions()
    {
        camera = new BoundCamera(0, 0, 800, 480);
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
                new RatioResolutionPolicy( this.getScreenResolutionRatio() ), this.camera);
        engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
        engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        return engineOptions;
    }

    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
    {
        ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
        ResourcesManager.getInstance();
        
        String lang = getPreferences(Context.MODE_PRIVATE).getString("language", "default");
        this.setLocale(lang);
        
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
    {
        SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
    }

    public void onPopulateScene( Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
    {
        SceneManager.getInstance().createMenuScene();
        
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }
    
    @Override
    public Engine onCreateEngine( EngineOptions pEngineOptions ) 
    {
        return new LimitedFPSEngine( pEngineOptions, 60 );
    }
    
    private float getScreenResolutionRatio() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics( dm );
        return ( (float) dm.widthPixels ) / ( ( float ) dm.heightPixels );
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
        }
        return false; 
    }
    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        
        if (this.isGameLoaded())
        {
            System.exit(0);    
        }
    }

    public void setLocale(String lang)
    {
        if (lang.equals("eng"))
        {
            Configuration c = new Configuration(getResources().getConfiguration());
            c.locale = Locale.ENGLISH;
            getResources().updateConfiguration(c,getResources().getDisplayMetrics());

        }
        else if (lang.equals("ger"))
        {
            Configuration c = new Configuration(getResources().getConfiguration());
            c.locale = Locale.GERMAN;
            getResources().updateConfiguration(c,getResources().getDisplayMetrics());

        }
        else if (lang.equals("rus"))
        {
            Configuration c = new Configuration(getResources().getConfiguration());
            c.locale = new Locale("ru","RU");
            getResources().updateConfiguration(c,getResources().getDisplayMetrics());
        }
        else
        {
            SharedPreferences prefrences = this.getPreferences(Context.MODE_PRIVATE);
            Editor ed = prefrences.edit();
            String locale;
            if (Locale.getDefault().getLanguage().compareTo("ru") == 0)
            {
            	locale = "rus";
            }
            else
            {
            	locale = "eng";
            }
            ed.putString("language", locale);
            ed.commit();
            
            this.setLocale(locale);
        }
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	if( ResourcesManager.getInstance().engine == null ) return;
    	
    	ResourcesManager.getInstance().onActivityResumed();
    	SceneManager.getInstance().onActivityResumed();
    }

    @Override
    protected void onPause() {
      super.onPause();

      ResourcesManager.getInstance().onActivityPaused();
      SceneManager.getInstance().onActivityPaused();
    }
 }
